package com.openjava.example.member.api;

import javax.annotation.Resource;

import com.openjava.example.order.service.ExampleOrderClient;
import com.openjava.example.order.domain.ExampleOrder;
import org.ljdp.common.bean.MyBeanUtils;
import org.ljdp.component.result.ApiResponse;
import org.ljdp.component.result.BasicApiResponse;
import org.ljdp.component.sequence.SequenceService;
import org.ljdp.component.sequence.ConcurrentSequence;
import org.ljdp.secure.annotation.Security;
import org.ljdp.ui.bootstrap.TablePage;
import org.ljdp.ui.bootstrap.TablePageImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import springfox.documentation.annotations.ApiIgnore;

import com.openjava.example.member.domain.ExampleMember;
import com.openjava.example.member.service.ExampleMemberService;
import com.openjava.example.member.query.ExampleMemberDBParam;


/**
 * api接口
 * @author 子右
 *
 */
@Api(tags="会员管理")
@RestController
@RequestMapping("/example/member/exampleMember")
public class ExampleMemberAction {
	
	@Resource
	private ExampleMemberService exampleMemberService;
	@Autowired
	private ExampleOrderClient exampleOrderClient;

    /**
     * 微服务：从交易中心获取订单
     * @param id
     * @return
     */
    @RequestMapping(value="/order/{id}",method=RequestMethod.GET)
    public ExampleOrder getOrder(@PathVariable("id")Long id) {
        return exampleOrderClient.get(id);
    }

    @RequestMapping(value="/order/hi",method=RequestMethod.GET)
    public ApiResponse hi(){
        return exampleOrderClient.hi();
    }

	/**
	 * 用主键获取数据
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据ID获取", notes = "单个对象查询", nickname="id")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主标识编码", required = true, dataType = "string", paramType = "path"),
	})
	@ApiResponses({
		@io.swagger.annotations.ApiResponse(code=20020, message="会话失效")
	})
	@Security(session=true)
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ExampleMember get(@PathVariable("id")String id) {
		ExampleMember m = exampleMemberService.get(id);
		return m;
	}
	
	@ApiOperation(value = "列表分页查询", notes = "{total：总数量，totalPage：总页数，rows：结果对象数组}", nickname="search")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "eq_memberId", value = "会员ID=", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "eq_memberAccount", value = "会员账号=", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "like_memberName", value = "会员名称like", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "eq_memberStatus", value = "会员状态=", required = false, dataType = "Long", paramType = "query"),
		@ApiImplicitParam(name = "le_registerTime", value = "注册时间<=", required = false, dataType = "Date", paramType = "query"),
		@ApiImplicitParam(name = "ge_registerTime", value = "注册时间>=", required = false, dataType = "Date", paramType = "query"),
		@ApiImplicitParam(name = "size", value = "每页显示数量", required = false, dataType = "int", paramType = "query"),
		@ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
	})
	@Security(session=false)
	@RequestMapping(value="/search",method=RequestMethod.GET)
	public TablePage<ExampleMember> doSearch(@ApiIgnore() ExampleMemberDBParam params, @ApiIgnore() Pageable pageable){
		Page<ExampleMember> result =  exampleMemberService.query(params, pageable);
		
		return new TablePageImpl<>(result);
	}

    @Security(session=false)
    @RequestMapping(value="/search2",method=RequestMethod.POST)
    public TablePage<ExampleMember> doSearch2(@RequestBody ExampleMemberDBParam params){
        Pageable pageable = PageRequest.of(0,10);
        Page<ExampleMember> result =  exampleMemberService.query(params, pageable);

        return new TablePageImpl<>(result);
    }
	
	/**
	 * 保存
	 */
	@ApiOperation(value = "保存", nickname="保存")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "memberId", value = "会员ID", required = false, dataType = "String", paramType = "post"),
		@ApiImplicitParam(name = "memberAccount", value = "会员账号", required = false, dataType = "String", paramType = "post"),
		@ApiImplicitParam(name = "memberName", value = "会员名称", required = false, dataType = "String", paramType = "post"),
		@ApiImplicitParam(name = "memberStatus", value = "会员状态", required = false, dataType = "Long", paramType = "post"),
		@ApiImplicitParam(name = "registerTime", value = "注册时间", required = false, dataType = "Date", paramType = "post"),
	})
	@Security(session=true)
	@RequestMapping(method=RequestMethod.POST)
	public ApiResponse doSave(@ApiIgnore() ExampleMember model, @RequestParam("isNew")Boolean isNew

			) {
		if(isNew) {
			//新增，记录创建时间等
			//设置主键(请根据实际情况修改)
			SequenceService ss = ConcurrentSequence.getInstance();
			model.setMemberId(ss.getSequence(""));
			ExampleMember dbObj = exampleMemberService.doSave(model);
		} else {
			//修改，记录更新时间等
			ExampleMember db = exampleMemberService.get(model.getMemberId());
			MyBeanUtils.copyPropertiesNotBlank(db, model);
			exampleMemberService.doSave(db);
		}
		
		BasicApiResponse resp = new BasicApiResponse();
		return resp;
	}
	
	@ApiOperation(value = "删除", nickname="delete")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主键编码", required = true, paramType = "post"),
	})
	@Security(session=true)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public ApiResponse doDelete(@RequestParam("id")String id) {
		exampleMemberService.doDelete(id);
		ApiResponse resp = new BasicApiResponse();
		return resp;
	}
	
	@ApiOperation(value = "批量删除", nickname="remove")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "ids", value = "主键编码用,分隔", required = true, paramType = "post"),
	})
	@Security(session=true)
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	public ApiResponse doRemove(@RequestParam("ids")String ids) {
		exampleMemberService.doRemove(ids);
		ApiResponse resp = new BasicApiResponse();
		return resp;
	}
	
}
